package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    public CheckItemService checkItemService;
    @Reference
    private CheckItemService addCheckItemService;

    @Reference
    private CheckItemService deleteCheckItemService;
    @PostMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){

        Result result = addCheckItemService.AddCheckItem(checkItem);
        return result;
    }

    @GetMapping("/delete")
    public Result deleteById(@RequestParam("id") Integer id){

        Result result = deleteCheckItemService.deleteCheckItem(id);
        return result;
    }
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkItemService.findPage(queryPageBean);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            Result result =new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItem);
            return result;
//            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @PostMapping("/edit")
    public Result update(@RequestBody CheckItem checkItem){
        try {
            checkItemService.update(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
    }
    @GetMapping("/findAll")
    public Result findById(){
        try {
            List<CheckItem> items = checkItemService.findAll();
            if (items.size()>0){
                Result result =new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
                result.setData(items);
                return result;
            }

//            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }

}
